package com.bayss.core.service.homepage;

import com.alibaba.fastjson.JSON;
import com.bayss.core.entity.goods.Sku;
import com.bayss.core.entity.goods.mapper.SkuMapper;
import com.bayss.core.entity.homepage.RecommendInfo;
import com.bayss.core.entity.homepage.RecommendInfoExample;
import com.bayss.core.entity.homepage.RotationChartExample;
import com.bayss.core.entity.homepage.mapper.RecommendInfoMapper;
import com.bayss.core.entity.station.Station;
import com.bayss.core.entity.station.mapper.StationMapper;
import com.bayss.core.service.sku.SkuService;
import com.bayss.core.util.CollectionUtils;
import com.bayss.core.util.FileUtil;
import com.github.pagehelper.PageHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * program: doko
 * author: bsworld.xie
 * create: 2019-11-10 16:13
 * description:
 */
@Service
public class RecommendService {
    private static Logger logger = LogManager.getLogger();

    @Autowired
    RecommendInfoMapper recommendInfoMapper;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private StationMapper stationMapper;

    /**
     * 新增推荐信息
     * @param recommendInfo
     */
    public void addRecommendInfo(RecommendInfo recommendInfo) {
        logger.info("addRecommendInfo(),recommendInfo:{}", JSON.toJSONString(recommendInfo));
        recommendInfo.setCreateTime(new Date());
        recommendInfo.setUpdateTime(new Date());
        recommendInfo.setStatus(1);
        recommendInfo.setBigPicCn(FileUtil.formatImgUrl(recommendInfo.getBigPicCn()));
        recommendInfo.setBigPicEn(FileUtil.formatImgUrl(recommendInfo.getBigPicEn()));
        recommendInfo.setBigPicKo(FileUtil.formatImgUrl(recommendInfo.getBigPicKo()));
        recommendInfo.setLittlePicCn(FileUtil.formatImgUrl(recommendInfo.getLittlePicCn()));
        recommendInfo.setLittlePicEn(FileUtil.formatImgUrl(recommendInfo.getLittlePicEn()));
        recommendInfo.setLittlePicKo(FileUtil.formatImgUrl(recommendInfo.getLittlePicKo()));
        recommendInfoMapper.insertSelective(recommendInfo);
    }

    /**
     * 修改推荐
     * @param recommendInfo
     */
    public void updateRecommendInfo(RecommendInfo recommendInfo) {
        logger.info("updateRecommendInfo(),recommendInfo:{}", JSON.toJSONString(recommendInfo));
        recommendInfo.setUpdateTime(new Date());
        recommendInfo.setBigPicCn(FileUtil.formatImgUrl(recommendInfo.getBigPicCn()));
        recommendInfo.setBigPicEn(FileUtil.formatImgUrl(recommendInfo.getBigPicEn()));
        recommendInfo.setBigPicKo(FileUtil.formatImgUrl(recommendInfo.getBigPicKo()));
        recommendInfo.setLittlePicCn(FileUtil.formatImgUrl(recommendInfo.getLittlePicCn()));
        recommendInfo.setLittlePicEn(FileUtil.formatImgUrl(recommendInfo.getLittlePicEn()));
        recommendInfo.setLittlePicKo(FileUtil.formatImgUrl(recommendInfo.getLittlePicKo()));
        recommendInfoMapper.updateByPrimaryKeySelective(recommendInfo);
    }

    /**
     *  删除推荐
     * @param id
     */
    public void deleteRecommendInfo(Integer id) {
        recommendInfoMapper.deleteByPrimaryKey(id);
    }

    /**
     * 状态更新
     * @param id
     * @param status
     */
    public void updateStatus(Integer id,Integer status) {
        RecommendInfo recommendInfo = new RecommendInfo();
        recommendInfo.setStatus(status);
        recommendInfo.setId(id);
        recommendInfoMapper.updateByPrimaryKeySelective(recommendInfo);
    }

    /**
     * 获取推荐详情
     * @param id
     * @return
     */
    public RecommendInfo getRecommendInfoById(Integer id) {
        logger.info("getRecommendInfoById(),id:{}", id);
        RecommendInfoExample example = new RecommendInfoExample();
        example.createCriteria().andIdEqualTo(id);
        List<RecommendInfo> recommendInfos = recommendInfoMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(recommendInfos)) {
            RecommendInfo recommendInfo = recommendInfos.get(0);
            return recommendInfo;
        }
        return null;
    }

    /**
     *
     * 根据权重值进行排序，权重值越小越靠前
     *
     * */
    public List<RecommendInfo> getRecommendInfoByPage(Integer status,Integer page, Integer pageSize) {
        logger.info("getRecommendInfoById(),page:{}, pageSize:{}", page, pageSize);
        RecommendInfoExample example = new RecommendInfoExample();
        if (status != null) {
            example.createCriteria().andStatusEqualTo(status);
        }
        example.setOrderByClause("weight asc,create_time DESC ");
        PageHelper.startPage(page, pageSize);
        List<RecommendInfo> rotationCharts = recommendInfoMapper.selectByExample(example);
        for (RecommendInfo r : rotationCharts) {
            Sku sku = skuMapper.selectByPrimaryKey(r.getSkuId().longValue());
            if (sku == null) {
                continue;
            }
            r.setSkuCode(sku.getSkuCode());
            r.setSkuNameCn(sku.getSkuNameCn());
        }
        return rotationCharts;
    }

}
